/*初始化datagrid*/
$(function () {
    /*抽取常用组件*/
    var role_list = $("#role_list");
    var role_dialog = $("#role_dialog");
    var role_form = $("#role_form");
    var selfPermission = $("#selfPermission");
    var allPermission = $("#allPermission");
    var allPermissionData;
    var selfPermissionData;
    /*对象管理方法*/
    var object = {
        add: function () {
            role_dialog.dialog('setTitle', '添加角色');
            role_form.form('clear');
            /*重置datagrid*/
            allPermission.datagrid('loadData', allPermissionData);
            selfPermission.datagrid('loadData', []);
            role_dialog.dialog('open');
        },
        edit: function () {
            var row = role_list.datagrid('getSelected');
            if (row == null) {
                $.messager.alert('温馨提示', '请选择一条数据!', 'info');
            } else {
                /*设置datagrid数据*/
                allPermission.datagrid('loadData', allPermissionData);
                selfPermission.datagrid('loadData', selfPermissionData);
                /*删除所有权限中重复的选项*/
                var ids = $.map(selfPermissionData.rows, function (item) {
                    return item.id;
                })
                var rows = allPermission.datagrid('getRows');
                for (i = rows.length - 1; i > -1; i--) {
                    if ($.inArray(rows[i].id, ids) > -1) {
                        allPermission.datagrid('deleteRow', i);
                    }
                }
                role_form.form('clear');
                role_form.form('load', row);
                role_dialog.dialog('setTitle', '编辑角色');
                role_dialog.dialog('open');
            }
        },
        save: function () {
            role_form.form('submit', {
                url: '/role/saveOrUpdate.do',
                onSubmit: function (param) {
                    var rows = selfPermission.datagrid('getRows');
                    for (i = 0; i < rows.length; i++) {
                        param['permissions[' + i + '].id'] = rows[i].id;
                    }
                },
                success: function (data) {
                    var res = $.parseJSON(data);
                    if (res.success) {
                        $.messager.alert('温馨提示', res.msg, 'info', function () {
                            role_dialog.dialog('close');
                            role_list.datagrid('reload');
                        });
                    } else {
                        $.messager.alert('温馨提示', res.msg, 'error');
                    }
                }
            });
        }, delete: function () {
            var row = role_list.datagrid('getSelected');
            if (row == null) {
                $.messager.alert('温馨提示', '请选择一条数据!', 'info');
                return;
            }
            $.messager.confirm('温馨提示', '确定要删除么?', function(ret){
                if (ret){
                    $.get('/role/delete.do', {id: row.id}, function (data) {
                        if (data.success) {
                            $.messager.alert('温馨提示', data.msg, 'info', function () {
                                role_list.datagrid('reload');
                            });
                        } else {
                            $.messager.alert('温馨提示', data.msg, 'error');
                        }
                    })
                }
            })
        },
        cancel: function () {
            role_dialog.dialog('close');
        },
        reload: function () {
            role_list.datagrid('reload');
        }
    }
    /*事件统一绑定*/
    $("[data-cmd]").click(function () {
        var cmd = $(this).data('cmd');
        object[cmd]();
    })

    role_list.datagrid({
        fit: true,
        rownumbers: true,
        fitColumns: true,
        pagination: true,
        singleSelect: true,
        toolbar: '#role_toolbar',
        url: '/role/query.do',
        columns: [[
            {field: 'name', title: '角色名', width: 100},
            {field: 'sn', title: '角色代码', width: 100},
            {field: 'permissionNames', title: '权限列表', width: 100},
        ]], onClickRow: function (index, row) {
            selfPermissionData = row.permissions;
        }
    })

    role_dialog.dialog({
        closed: true,
        width: 450,
        height: 420,
        buttons: $("#role_diaButtons"),
    })

    /*初始化已有权限*/
    selfPermission.datagrid({
        rownumbers: true,
        fitColumns: true,
        singleSelect: true,
        data: [],
        columns: [[
            {field: 'name', title: '权限名称', width: 100}
        ]],
        onClickRow: function (index, row) {
            allPermission.datagrid('appendRow', row);
            selfPermission.datagrid('deleteRow', index);
        }, onLoadSuccess: function (data) {
            selfPermissionData = $.extend(true, {}, data);
        }
    })
    /*初始化所有权限*/
    allPermission.datagrid({
        rownumbers: true,
        fitColumns: true,
        singleSelect: true,
        url: '/permission/listAll.do',
        columns: [[
            {field: 'name', title: '权限名称', width: 100}
        ]],
        onLoadSuccess: function (data) {
            allPermissionData = $.extend(true, {}, data);
        },
        onClickRow: function (index, row) {
            selfPermission.datagrid('appendRow', row);
            allPermission.datagrid('deleteRow', index);
        }
    })
})
